﻿using Newtonsoft.Json;
using ReactiveUI.Fody.Helpers;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;

namespace Common.ViewModels
{
    /// <summary>
    /// 变换
    /// </summary>
    [Serializable]
    [JsonObject(MemberSerialization.OptIn)]
    public class TranslateCoff : ViewModelBase
    {

        private IDisposable m_DisPosition;

        private IPosition m_IPosition;

        [JsonProperty]
        public IPosition IPosition
        {
            get => m_IPosition;
            set
            {
                m_DisPosition?.Dispose();
                m_IPosition = value;
                m_DisPosition = m_IPosition?.WhenAnyValue(x => x.Position).Skip(1).Subscribe(UpdatePosition);
            }
        }

        [Reactive] public List<IPosition> Positions { get; set; }


        /// <summary>
        /// 偏移量
        /// </summary>
        [Reactive] public double Offset { get; set; }

        /// <summary>
        /// 起点偏移量
        /// </summary>
        [Reactive] public double StartOffset { get; set; }

        /// <summary>
        /// 终点点偏移量
        /// </summary>
        [Reactive] public double EndOffset { get; set; }

        /// <summary>
        /// 编码器起点
        /// </summary>
        [Reactive]
        public double EncoderStart { get; set; }

        /// <summary>
        /// 编码器终点
        /// </summary>
        [Reactive]
        public double EncoderEnd { get; set; }

        [Reactive]
        [JsonProperty]
        public double 常量 { get; set; }

        [Reactive]
        [JsonProperty]
        public double Coff { get; set; }




        /// <summary>
        /// 两点计算系数
        /// </summary>
        public void 计算系数()
        {
            Coff = (EndOffset - StartOffset) / (EncoderEnd - EncoderStart);
            常量 = StartOffset - (EncoderStart * Coff);
        }

        public ReactiveCommand<Unit, Unit> Command计算系数 => ReactiveCommand.Create(计算系数);

        public ReactiveCommand<Unit, Unit> Command查找IPosition => ReactiveCommand.Create(FillIPosition);


        /// <summary>
        /// 更新位置
        /// </summary>
        /// <param name="position"></param>
        public void UpdatePosition(double position)
        {
            this.Offset = 常量 + position * Coff;

        }

        public TranslateCoff()
        {
            //FillIPosition();
            //this.Positions= new List<IPosition>();
        }

        /// <summary>
        /// 在Loaded里面写
        /// </summary>
        public void FillIPosition()
        {

            List<IPosition> lisPositions = new List<IPosition>();

            //foreach (var item in ProductLine.Instance.BoatVms)
            //{
            //    lisPositions.Add(item);

            //}

            //foreach (IPosition position in ProductLine.Instance.Axiss)
            //{
            //    lisPositions.Add(position);
            //}

            //foreach (Car car in ProductLine.Instance.Cars)
            //{
            //    lisPositions.Add(car);
            //}
            this.Positions = lisPositions;
        }

        /// <summary>
        /// 
        /// </summary>
        public void Clear()
        {
            this.IPosition = null;
        }


    }
}
